package com.ruoyi.miniapp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.miniapp.domain.MiniappPost;
import com.ruoyi.miniapp.service.IMiniappPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息Controller
 *
 * @author ruoyi
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/post")
public class MiniappPostController extends BaseController
{
    @Autowired
    private IMiniappPostService miniappPostService;

    /**
     * 查询岗位信息列表
     */
    @RequiresPermissions("miniapp:post:list")
    @GetMapping("/list")
    public TableDataInfo list(MiniappPost miniappPost)
    {
        startPage();
        List<MiniappPost> list = miniappPostService.selectMiniappPostList(miniappPost);
        return getDataTable(list);
    }

    /**
     * 导出岗位信息列表
     */
    @RequiresPermissions("miniapp:post:export")
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniappPost miniappPost)
    {
        List<MiniappPost> list = miniappPostService.selectMiniappPostList(miniappPost);
        ExcelUtil<MiniappPost> util = new ExcelUtil<MiniappPost>(MiniappPost.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    /**
     * 获取岗位信息详细信息
     */
    @RequiresPermissions("miniapp:post:query")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return success(miniappPostService.selectMiniappPostByPostId(postId));
    }

    /**
     * 新增岗位信息
     */
    @RequiresPermissions("miniapp:post:add")
    @Log(title = "岗位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniappPost miniappPost)
    {
        if (!miniappPostService.checkPostNameUnique(miniappPost))
        {
            return error("新增岗位'" + miniappPost.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!miniappPostService.checkPostCodeUnique(miniappPost))
        {
            return error("新增岗位'" + miniappPost.getPostName() + "'失败，岗位编码已存在");
        }
        miniappPost.setCreateBy(SecurityUtils.getUsername());
        return toAjax(miniappPostService.insertMiniappPost(miniappPost));
    }

    /**
     * 修改岗位信息
     */
    @RequiresPermissions("miniapp:post:edit")
    @Log(title = "岗位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniappPost miniappPost)
    {
        if (!miniappPostService.checkPostNameUnique(miniappPost))
        {
            return error("修改岗位'" + miniappPost.getPostName() + "'失败，岗位名称已存在");
        }
        else if (!miniappPostService.checkPostCodeUnique(miniappPost))
        {
            return error("修改岗位'" + miniappPost.getPostName() + "'失败，岗位编码已存在");
        }
        miniappPost.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(miniappPostService.updateMiniappPost(miniappPost));
    }

    /**
     * 删除岗位信息
     */
    @RequiresPermissions("miniapp:post:remove")
    @Log(title = "岗位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(miniappPostService.deleteMiniappPostByPostIds(postIds));
    }
}
